import 'package:amap_location/amap_location.dart';
import 'package:flutter/material.dart';
//import 'package:amap_location/amap_location.dart';
//import 'package:simple_permissions/simple_permissions.dart';
class LocationPage extends StatefulWidget {
  LocationPage({Key key}) : super(key: key);
  @override
  _LocationPageState createState() => _LocationPageState();
}

class _LocationPageState extends State<LocationPage> {

  AMapLocation _loc;
  double lon;
  double lan;
  //检测定位权限
  // void _checkPersmission() async {
  //   bool hasPermission =
  //   await SimplePermissions.checkPermission(Permission.WhenInUseLocation);
  //   if (!hasPermission) {
  //     PermissionStatus requestPermissionResult =await SimplePermissions.requestPermission( Permission.WhenInUseLocation);
  //     if (requestPermissionResult != PermissionStatus.authorized) {
  //       // Alert.alert(context, title: "申请定位权限失败");
  //       print("申请定位权限失败");
  //       return;
  //     }
  //   }
  //   AMapLocation loc = await AMapLocationClient.getLocation(true);
  //   print("经度：${loc.longitude}");
  //   print("玮度：${loc.latitude}");
  //   setState(() {
  //     _loc = loc;
  //   });
  // }

  @override
  void initState() {
     //_checkPersmission();
    super.initState();
    this._getLocation();
  }

  // @override
  // void dispose() {
  //   //这里可以停止定位
  //   AMapLocationClient.stopLocation();
  //   super.dispose();
  // }

  _getLocation () async {
    // //先启动一下
     await AMapLocationClient.startup(new AMapLocationOption( desiredAccuracy:CLLocationAccuracy.kCLLocationAccuracyHundredMeters  ));
     //直接获取定位:
     var result =  await AMapLocationClient.getLocation(true);
     print("经度：${result.longitude}");
     print("玮度：${result.latitude}");
     setState(() {
       lon = result.longitude;
       lan = result.latitude;
     });
    // });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("获取定位信息"),
      ),
      body: Center(
        child: Column(
          children: <Widget>[
            Text("$lon"),
            Text("$lan"),
          ],
        ),
      ),
    );
  }
}
